package com.weng.starter.kafka.sample1.config;

import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.support.converter.RecordMessageConverter;
import org.springframework.kafka.support.converter.StringJsonMessageConverter;

/**
*
* @author wengzhonghui
* @date: 2021-06-23 14:48:39
*/
@Configuration
public class KafkaConfig implements InitializingBean {

    @Value("${kafka.topic.my-topic}")
    String myTopic;
    public static String MY_TOPTIC_TEST_1 = "1";


    @Value("${kafka.topic.my-topic2}")
    String myTopic2;
    public static String MY_TOPTIC_TEST_2 = "1";




    /**
     * JSON消息转换器
     */
    @Bean
    public RecordMessageConverter jsonConverter() {
        return new StringJsonMessageConverter();
    }

    /**
     * 通过注入一个 NewTopic 类型的 Bean 来创建 topic，如果 topic 已存在，则会忽略。
     */
    @Bean
    public NewTopic myTopic() {
        return new NewTopic(MY_TOPTIC_TEST_1, 2, (short) 1);
    }

    @Bean
    public NewTopic myTopic2() {
        return new NewTopic(MY_TOPTIC_TEST_2, 1, (short) 1);
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        MY_TOPTIC_TEST_1 = myTopic;
        MY_TOPTIC_TEST_2 = myTopic2;
    }
}
